var table = null;
layui
    .config({
        base: '../layui/exts/', //静态资源所在路径
    })
    .use(['table', 'upload', 'util'], function () {
        lui = layui;
        var form = layui.form;
        var upload = layui.upload;
        var util = layui.util;
        $ = layui.$;

        table = layui.table;
        openData();

        function openData(obj) {
            let param = {};
            if (obj != undefined) {
                Object.assign(param, obj);
            }
            table.render({
                elem: '#dataTable'
                , url: '/caes/getCustomerData'
                , method: 'POST'
                , contentType: "application/json"
                , cellMinWidth: 50
                , page: true
                , limit: 50
                , height: 'full-110'
                , loading: true
                , size: 'sm'
                , where: param
                , cols: [
                    [ //标题栏
                        {field: 'code', title: '客户编号', width: 120, fixed: 'left'},
                        {field: 'name', title: '客户名称', width: 220, fixed: 'left'},
                        {field: 'category', title: '用电类别', width: 110},
                        {field: 'usageNature', title: '用电性质', width: 110},
                        {field: 'premiseAddress', title: '用电地址', width: 200},
                        {field: 'postAddress', title: '通邮地址', width: 200},
                        {field: 'postCode', title: '邮政编码', width: 100},
                        {field: 'fax', title: '传真', width: 100},
                        {field: 'email', title: '电子邮箱', width: 150},
                        {field: 'customerIdentity', title: '客户身份', width: 100},
                        {field: 'idNumber', title: '证件号', width: 150},
                        {field: 'contact', title: '联系人', width: 110},
                        {field: 'contactPhone', title: '联系人电话', width: 110},
                        {field: 'powerSupplier', title: '供电单位', width: 200},
                        {field: 'districtName', title: '台区名称', width: 110},
                        {field: 'remarks', title: '备注', width: 150},
                        {field: 'creator', title: '创建人', width: 80},
                        {field: 'createTime', title: '创建时间', width: 120},
                        {field: 'modifier', title: '修改人', width: 80},
                        {field: 'modifyTime', title: '修改时间', width: 120},
                        {fixed: 'right', title: '操作', width: 80, toolbar: '#operationBar'}
                    ]
                ]
            });
        }


        //监听工具条
        table.on('tool(dataTable)', function (obj) {
            var data = obj.data;
            switch (obj.event) {
                case "edit":
                    top.layer.open({
                        type: 2,
                        title: '编辑客户信息',
                        content: './caes/setCustomerInfo?id=' + data.id,
                        area: ['550px', $(parent.window).height() + 'px'],
                        offset: 'rt',
                        shadeClose: false,
                        resize: false,
                        btn: [
                            '<i class="layui-icon">&#xe605;</i> 确定',
                            '<i class="layui-icon">&#x1006;</i> 取消',
                        ],
                        yes: function (index, layero) {
                            var body = layer.getChildFrame('body', index);
                            var iframeWin = layero.find('iframe')[0];
                            if (iframeWin.contentWindow.Submit()) {
                                table.reload('dataTable');
                            }
                        },
                        btn2: function (index, layero) {
                            layer.close(index); //如果设定了yes回调，需进行手工关闭
                        },
                    });
                    return false;
                case 'del':
                    top.layer.confirm('确定要删除当前选择的客户吗？ ', function (index) {
                        $.ajax({
                            url: '../caes/delCustomer', //url
                            dataType: 'json', //数据格式
                            type: 'POST', //请求方式
                            async: false, //是否异步请求
                            contentType: "application/json",
                            data: JSON.stringify({id: data.id}),
                            success: function (result) {
                                if (result.code == 0) {
                                    table.reload('dataTable');
                                    layer.close(index);
                                    top.parent.layer.msg(result.message, {icon: 1});
                                } else {
                                    top.parent.layer.alert(result.message, {icon: 2});
                                }
                            },
                        });
                        return false;
                    });
                    return false;
            }
        });

        util.on({
            newCustomer: function () {
                top.layer.open({
                    type: 2,
                    title: '编辑客户信息',
                    content: './caes/setCustomerInfo?id=',
                    area: ['550px', $(parent.window).height() + 'px'],
                    offset: 'rt',
                    shadeClose: false,
                    resize: false,
                    btn: [
                        '<i class="layui-icon">&#xe605;</i> 确定',
                        '<i class="layui-icon">&#x1006;</i> 取消',
                    ],
                    yes: function (index, layero) {
                        var body = layer.getChildFrame('body', index);
                        var iframeWin = layero.find('iframe')[0];
                        if (iframeWin.contentWindow.Submit()) {
                            table.reload('dataTable');
                        }
                    },
                    btn2: function (index, layero) {
                        layer.close(index); //如果设定了yes回调，需进行手工关闭
                    },
                });
                return false;
            }

        });

    });
